package com.casic.ops.controller;

import com.casic.ops.annotation.EventLog;
import com.casic.ops.annotation.IgnoreAuth;
import com.casic.ops.annotation.RestWrapper;
import com.casic.ops.consts.event.EventType;
import com.casic.ops.entity.request.UserLoginRequest;
import com.casic.ops.entity.request.UserResetRequest;
import com.casic.ops.entity.vo.UserInfoVO;
import com.casic.ops.entity.vo.UserLoginVO;
import com.casic.ops.service.api.PassportService;
import com.casic.ops.utils.Currents;
import com.casic.ops.utils.OpsAssert;
import com.orion.lang.wrapper.HttpWrapper;
import com.orion.servlet.web.Servlets;
import com.orion.utils.Objects1;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName ops-platform
 * @Description 认证
 * @Author mianbin
 * @Date 2022/5/31 10:58
 * @Version 1.0
 **/
@RestController
@RestWrapper
@RequestMapping("/ops/api/auth")
public class AuthenticateController {

    @Resource
    private PassportService passportService;

    /**
     * 登录
     */
    @RequestMapping("/login")
    @IgnoreAuth
    @EventLog(EventType.LOGIN)
    public UserLoginVO login(@RequestBody UserLoginRequest login, HttpServletRequest request) {
        System.out.println("==========测试前端登录接口能否进来===============");
        String username = OpsAssert.notBlank(login.getUsername()).trim();
        String password = OpsAssert.notBlank(login.getPassword()).trim();
        login.setUsername(username);
        login.setPassword(password);
        System.out.println("==================登录=======================");
        System.out.println(username);
        System.out.println(password);
        System.out.println("=====================登录======================");
        login.setIp(Servlets.getRemoteAddr(request));
        // 登录
        return passportService.login(login);
    }

    @PostMapping("/tologin")
    @IgnoreAuth
    @EventLog(EventType.LOGIN)
    public UserLoginVO tologin(@RequestBody UserLoginRequest login, HttpServletRequest request){
        System.out.println("这些新写的login接口");
        UserLoginVO userLoginVo=new UserLoginVO();
        userLoginVo.setUserId(1L);
        userLoginVo.setUsername("admin");
        userLoginVo.setNickname("管理员");
        userLoginVo.setToken("wNYvIBR67satExqnYy8IkdxEpxiSevbZ");
        userLoginVo.setRoleType(10);
        return userLoginVo;
    }

    /**
     * 登出
     */
    @RequestMapping("/logout")
    @IgnoreAuth
    @EventLog(EventType.LOGOUT)
    public HttpWrapper<?> logout() {
        passportService.logout();
        return HttpWrapper.ok();
    }

    /**
     * 重置密码
     */
    @RequestMapping("/reset")
    @EventLog(EventType.RESET_PASSWORD)
    public HttpWrapper<?> resetPassword(@RequestBody UserResetRequest request) {
        String password = OpsAssert.notBlank(request.getPassword()).trim();
        request.setUserId(Objects1.def(request.getUserId(), Currents::getUserId));
        request.setPassword(password);
        passportService.resetPassword(request);
        return HttpWrapper.ok();
    }

    /**
     * 检查用户信息
     */
    @IgnoreAuth
    @RequestMapping("/valid")
    public UserInfoVO validToken() {
        UserInfoVO userInfoVO= new UserInfoVO();
        userInfoVO.setId(1L);
        userInfoVO.setUsername("admin");
        userInfoVO.setNickname("叫我B哥");
        userInfoVO.setEmail("wuge4615@qq.com");
        userInfoVO.setPhone("13929741214");
        userInfoVO.setLocked(1);
        return userInfoVO;
    }

}
